package com.gitee.huxing.course.conc.task;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.conc.task
 * @ClassName: TaskService01Impl
 * @Author: huxing
 * @DateTime: 2021-08-25 下午4:58
 */
public class TaskThreadPool10 extends AbstractTaskThreadPool{

    public static AtomicInteger atomicInteger = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException{
        new TaskThreadPool10().task();
    }

    @Override
    public int threadTask() {
        CustomThread thread = new CustomThread(this);
        try {
            // 启动线程
            thread.start();
            // 线程等待
            synchronized (thread){
                thread.wait();
            }
        } catch (Exception ex){
            ex.printStackTrace();
        }
        return atomicInteger.get();
    }

    static class CustomThread extends Thread{

        public AbstractTaskThreadPool taskThreadPool;

        public CustomThread(AbstractTaskThreadPool taskThreadPool){
            this.taskThreadPool = taskThreadPool;
        }

        @Override
        public void run() {
            System.out.println("===>" + "计算数据");
            atomicInteger.set(CourseTask.sum());
            System.out.println("===>" + "唤醒主线程");
        }
    }
}
